package com.jz.community.moduleshoppingguide.home.ui.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.commview.view.MyStateView;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.home.bean.ChunnelNewbornBean;
import com.jz.community.moduleshoppingguide.home.presenter.NewbornPresenter;
import com.jz.community.moduleshoppingguide.home.ui.NewbornView;
import com.jz.community.moduleshoppingguide.home.ui.adapter.NewbornAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;

public class NewbornActivity extends BaseMvpActivity<NewbornView.View, NewbornPresenter> implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, NewbornView.View {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.rv_newborn)
    RecyclerView rvNewborn;
    @BindView(R2.id.newborn_refresh)
    SmartRefreshLayout newbornRefresh;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;

    private int page = 0;
    private int size = 10;
    MyStateView myStateView;
    private View noDataView;
    private View errorView;
    private View loadingView;
    NewbornAdapter newbornAdapter;

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected int getContentView() {
        return R.layout.module_shoppingguide_activity_newborn;
    }

    @Override
    protected NewbornPresenter createPresenter() {
        return new NewbornPresenter(this);
    }


    @Override
    protected void initView() {
        titleName.setText("新人专享");
        setImmersionBar(titleToolbar);
        titleBackLeft.setOnClickListener(v -> finish());
        myStateView = new MyStateView();
        noDataView = myStateView.getNoDataView(this, (ViewGroup) rvNewborn.getParent(), "暂无优惠券！", null);
        loadingView = myStateView.getLoadingView(this, (ViewGroup) rvNewborn.getParent(), R.drawable.loading_default);
        errorView = myStateView.getErrorView(this, (ViewGroup) rvNewborn.getParent(), v -> {
            newbornAdapter.setEmptyView(loadingView);
            getData(AppConstants.ONREFRESH);
        });
    }

    @Override
    protected void initDatas() {
        newbornRefresh.setOnRefreshListener(this);
        newbornRefresh.setEnableLoadMore(false);
        rvNewborn.setLayoutManager(new LinearLayoutManager(this));
        newbornAdapter = new NewbornAdapter(R.layout.module_shoppingguide_item_home_type_xrzq_item, new ArrayList<>());
        rvNewborn.setAdapter(newbornAdapter);
        newbornAdapter.setOnLoadMoreListener(this, rvNewborn);
        newbornAdapter.setEmptyView(loadingView);
        getData(AppConstants.ONREFRESH);
    }


    @Override
    public void showChunnelNewborn(ChunnelNewbornBean chunnelNewbornBean, int type) {
        newbornRefresh.finishRefresh();
        if (chunnelNewbornBean != null && chunnelNewbornBean.get_embedded() != null && chunnelNewbornBean.get_embedded().getContent() != null) {
            if (chunnelNewbornBean.get_embedded().getContent().size() > 0) {
                if (type == AppConstants.ONREFRESH) {
                    newbornAdapter.setNewData(chunnelNewbornBean.get_embedded().getContent());
                } else {
                    newbornAdapter.addData(chunnelNewbornBean.get_embedded().getContent());
                    newbornAdapter.loadMoreComplete();
                }
            } else {
                if (type == AppConstants.ONLOADMORE) {
                    newbornAdapter.loadMoreEnd();
                }
            }
        } else {
            if (type == AppConstants.ONLOADMORE) {
                newbornAdapter.loadMoreEnd();
            }
        }
        if (newbornAdapter.getItemCount() == 0) {
            newbornAdapter.setEmptyView(noDataView);
        }
    }

    @Override
    public void showError(String s, int type) {
        newbornAdapter.setEmptyView(errorView);
    }


    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        getData(AppConstants.ONREFRESH);
    }

    @Override
    public void onLoadMoreRequested() {
        getData(AppConstants.ONLOADMORE);
    }

    private void getData(int type) {
        if (type == AppConstants.ONREFRESH) {
            page = 0;
            mPresenter.getChunnelNewborn(page, size, AppConstants.ONREFRESH);
        } else {
            page++;
            mPresenter.getChunnelNewborn(page, size, AppConstants.ONLOADMORE);
        }
    }

}
